package com.minhua.jijiehao2.mainframe.launcher.guideactivity.impl

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.common.imagefragment.impl.ImageFragment
import com.minhua.jijiehao2.mainframe.launcher.guideactivity.imp.IGuideView
import com.minhua.jijiehao2.mainframe.main.impl.MainActivity
import com.minhua.jijiehao2.utils.SPUtils
import kotlinx.android.synthetic.main.activity_guide.*

internal class GuideActivity : BaseActivity<IGuideView, GuidePresenter>(), IGuideView {

    override fun getLayoutId(): Int {
        return R.layout.activity_guide
    }

    override fun getPresenter(): GuidePresenter {
        return GuidePresenter()
    }

    override fun initView() {
        val fragments = listOf<Fragment>(
                ImageFragment.newInstance(R.mipmap.guide_bg1)
                , ImageFragment.newInstance(R.mipmap.guide_bg2)
                , ImageFragment.newInstance(R.mipmap.guide_bg3))
        vpGuideContent.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragments[position]

            override fun getCount(): Int = fragments.size
        }

        vpGuideContent.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == (fragments.size - 1)) {
                    tvGuideGo.visibility = View.VISIBLE
                } else {
                    tvGuideGo.visibility = View.GONE
                }
            }
        })

        tvGuideGo.setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
            this.finish()
        }
    }

    override fun onBackPressed() {
        //屏蔽返回
    }

    override fun onDestroy() {
        super.onDestroy()
        SPUtils.put(DefineKey.FIRST_RUN_APP, false)
    }
}
